package com.sugar.follow.up.module.business.service.doctor;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.doctor.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.doctor.DoctorDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.doctor.DoctorMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 医生 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class DoctorServiceImpl implements DoctorService {

    @Resource
    private DoctorMapper doctorMapper;

    @Override
    public Long createDoctor(DoctorSaveReqVO createReqVO) {
        // 插入
        DoctorDO doctor = BeanUtils.toBean(createReqVO, DoctorDO.class);
        doctorMapper.insert(doctor);

        // 返回
        return doctor.getId();
    }

    @Override
    public void updateDoctor(DoctorSaveReqVO updateReqVO) {
        // 校验存在
        validateDoctorExists(updateReqVO.getId());
        // 更新
        DoctorDO updateObj = BeanUtils.toBean(updateReqVO, DoctorDO.class);
        doctorMapper.updateById(updateObj);
    }

    @Override
    public void deleteDoctor(Long id) {
        // 校验存在
        validateDoctorExists(id);
        // 删除
        doctorMapper.deleteById(id);
    }

    @Override
        public void deleteDoctorListByIds(List<Long> ids) {
        // 删除
        doctorMapper.deleteByIds(ids);
        }


    private void validateDoctorExists(Long id) {
        if (doctorMapper.selectById(id) == null) {
            throw exception(DOCTOR_NOT_EXISTS);
        }
    }

    @Override
    public DoctorDO getDoctor(Long id) {
        return doctorMapper.selectById(id);
    }

    @Override
    public PageResult<DoctorDO> getDoctorPage(DoctorPageReqVO pageReqVO) {
        return doctorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DoctorRespVO> getDoctorAllList() {
        return BeanUtils.toBean(doctorMapper.selectList(), DoctorRespVO.class);
    }

}